package com.smart.dao;

/**
 * Created by Jack on 2015/1/1.
 */

import com.smart.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import  org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;


@Repository
public class UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public  int  getMatchCount(int id, String password){
        String sqlStr = "select count(*) from user "
                + " where id=? and password=? ";
        return jdbcTemplate.queryForInt(sqlStr,new Object[]{
                id,password
        });

    }

    public  User getUser(final int id) {
       final User user = new User();
        String sqlStr = "select * from user WHERE id=?";
        jdbcTemplate.query(sqlStr, new Object[]{id}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {

                user.setNickname(resultSet.getString("nickname"));
                user.setAddress(resultSet.getString("address"));
                user.setId(id);
                user.setPassword(resultSet.getString("password"));
            }
        });
        return user;
    }




}
